package com.base.cn.platform.os.controller.course.note;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.course.note.CourseNote;
import com.base.cn.platform.os.service.course.note.CourseNoteBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 课程笔记
 *
 * @author SK
 * @since 2018-06-19
 **/
@RestController
@RequestMapping("/course")
public class CourseNoteController extends BaseController {

    @Autowired
    private CourseNoteBiz courseNoteBiz;

    /**
     * 保存课程笔记
     *
     * @param courseNote 课程笔记
     * @return 课程笔记
     */
    @RequestMapping("/saveCourseNote")
    public Map<String, Object> saveCourseNote(@RequestBody CourseNote courseNote) {
        Map<String, Object> check = this.checkCourseNote(courseNote);
        if (check != null) {
            return check;
        }
        courseNoteBiz.saveCourseNote(courseNote);
        return ResultUtil.SUCCESS("保存成功");
    }

    /**
     * 查询课程笔记列表
     *
     * @param courseNote  查询条件
     * @param count       数量
     * @param setUser     设置用户
     * @param setCourse   设置课程
     * @param setCatalog  设置章节
     * @param setDescribe 设置描述信息 xx > xx hh:mm:ss
     * @return 课程笔记列表
     */
    @RequestMapping("/findCourseNoteList")
    public List<CourseNote> findCourseNoteList(@RequestBody CourseNote courseNote,
                                               @RequestParam(value = "count", required = false) Integer count,
                                               @RequestParam("setUser") boolean setUser,
                                               @RequestParam("setCourse") boolean setCourse,
                                               @RequestParam("setCatalog") boolean setCatalog,
                                               @RequestParam("setDescribe") boolean setDescribe) {
        return courseNoteBiz.findCourseNoteList(courseNote, count, setUser, setCourse, setCatalog, setDescribe);
    }

    /**
     * 查询课程笔记列表
     *
     * @param courseNote  查询条件
     * @param currentPage 第N页
     * @param pageSize    每页数据量
     * @param setUser     设置用户
     * @param setCourse   设置课程
     * @param setCatalog  设置章节
     * @param setDescribe 设置描述信息 xx > xx hh:mm:ss
     * @return 课程笔记列表
     */
    @RequestMapping("/findCourseNotePage")
    public PageInfo<CourseNote> findCourseNotePage(@RequestBody CourseNote courseNote,
                                                   @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                                   @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                                   @RequestParam("setUser") boolean setUser,
                                                   @RequestParam("setCourse") boolean setCourse,
                                                   @RequestParam("setCatalog") boolean setCatalog,
                                                   @RequestParam("setDescribe") boolean setDescribe) {
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        if (DataUtil.isPositive(pageSize)) {
            page.setPageSize(pageSize);
        }
        return courseNoteBiz.findCourseNotePage(courseNote, page, setUser, setCourse, setCatalog, setDescribe);
    }

    /**
     * 检查课程笔记参数
     *
     * @param courseNote 课程笔记
     */
    private Map<String, Object> checkCourseNote(CourseNote courseNote) {
        if (!DataUtil.idIsNotNull(courseNote.getCourseId())
                || !DataUtil.idIsNotNull(courseNote.getCatalogId())) {
            return ResultUtil.ERROR("参数错误");
        }
        if (!DataUtil.idIsNotNull(courseNote.getUserId())) {
            return ResultUtil.ERROR("未登录");
        }
        if (!DataUtil.isPositive(courseNote.getProgress())) {
            return ResultUtil.ERROR("观看时长不足");
        }
        if (StringUtils.isEmpty(courseNote.getNote()) || courseNote.getNote().trim().length() < 12) {
            return ResultUtil.ERROR("笔记内容最少12个字");
        }
        return null;
    }
}
